package com.ifast.data.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ifast.common.base.AdminBaseController;
import com.ifast.data.domain.DataPageinfoDO;
import com.ifast.data.domain.DataStrokeDO;
import com.ifast.data.service.DataPageinfoService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 首页配置信息
 * </pre>
 * <small> 2018-04-16 17:32:51 | lvyq</small>
 */
@Controller
@RequestMapping("/data/dataPageinfo")
public class DataPageinfoController extends AdminBaseController {
	@Autowired
	private DataPageinfoService dataPageinfoService;
	
	@GetMapping()
	@RequiresPermissions("data:dataPageinfo:dataPageinfo")
	String DataPageinfo(){
	    return "data/dataPageinfo/dataPageinfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataPageinfo:dataPageinfo")
	public Result<Page<DataPageinfoDO>> list(DataPageinfoDO dataPageinfoDTO){
        Wrapper<DataPageinfoDO> wrapper = new EntityWrapper<DataPageinfoDO>(dataPageinfoDTO);
        Page<DataPageinfoDO> page = dataPageinfoService.selectPage(getPage(DataPageinfoDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("data:dataPageinfo:add")
	String add(){
	    return "data/dataPageinfo/add";
	}

	@GetMapping("/edit/{infoid}")
	@RequiresPermissions("data:dataPageinfo:edit")
	String edit(@PathVariable("infoid") Long infoid,Model model){
		DataPageinfoDO dataPageinfo = dataPageinfoService.selectById(infoid);
		model.addAttribute("dataPageinfo", dataPageinfo);
	    return "data/dataPageinfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataPageinfo:add")
	public Result<String> save( DataPageinfoDO dataPageinfo){
		dataPageinfoService.insert(dataPageinfo);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataPageinfo:edit")
	public Result<String>  update( DataPageinfoDO dataPageinfo){
		dataPageinfoService.updateAllColumnById(dataPageinfo);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("data:dataPageinfo:remove")
	public Result<String>  remove( Long infoid){
		dataPageinfoService.deleteById(infoid);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataPageinfo:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] infoids){
		dataPageinfoService.deleteBatchIds(Arrays.asList(infoids));
		return Result.ok();
	}
	
	@GetMapping("/api/v1/getPageinfo")
	@ResponseBody
	public Result<Map<String,Object>> getPageinfo(DataPageinfoDO dataPageinfo) {
		Wrapper<DataPageinfoDO> wrapper = new EntityWrapper<DataPageinfoDO>(dataPageinfo);
		Map<String,Object> PageinfoMap = dataPageinfoService.selectMap(wrapper);
		if(PageinfoMap!=null&&PageinfoMap.size()>0) {
			return Result.ok(PageinfoMap);
		}
		return Result.ok();
	}
	
	@GetMapping("/api/v1/getPageinfo2")
	@ResponseBody
	public JSONPObject getPageinfo(String jsonpCallback,DataPageinfoDO dataPageinfo) {
		/*Wrapper<DataPageinfoDO> wrapper = new EntityWrapper<DataPageinfoDO>(dataPageinfo);
		Map<String,Object> PageinfoMap = dataPageinfoService.selectMap(wrapper);
		
		MappingJacksonValue mappingJacksonValue = null;
		if(PageinfoMap!=null&&PageinfoMap.size()>0) {
			mappingJacksonValue = new MappingJacksonValue(Result.ok(Result.ok(PageinfoMap)));
		}else {
			mappingJacksonValue = new MappingJacksonValue(Result.ok());
		}
	    mappingJacksonValue.setJsonpFunction(jsonpCallback);
	    return mappingJacksonValue;*/
		
		Wrapper<DataPageinfoDO> wrapper = new EntityWrapper<DataPageinfoDO>(dataPageinfo);
		Map<String,Object> PageinfoMap = dataPageinfoService.selectMap(wrapper);
		if(PageinfoMap!=null&&PageinfoMap.size()>0) {
			return new JSONPObject(jsonpCallback, Result.ok(PageinfoMap));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
		
	}
	
}
